package org.example.coupon.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.core.utils.PageUtils;
import org.example.coupon.api.ProductService;
import org.example.coupon.api.WareHouseService;
import org.example.coupon.api.module.SkuInfo;
import org.example.coupon.bo.SeckillSkuRelationBo;
import org.example.coupon.dao.SeckillSkuRelationDao;
import org.example.coupon.entity.SeckillSkuRelationEntity;
import org.example.coupon.service.SeckillPromotionService;
import org.example.coupon.to.SkuSeckillTo;
import org.example.coupon.vo.SeckillSkuRelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.example.coupon.service.SeckillSkuRelationService;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("seckillSkuRelationService")
public class SeckillSkuRelationServiceImpl implements SeckillSkuRelationService {

    @Autowired
    private SeckillSkuRelationDao seckillSkuRelationDao;

    @Autowired
    private ProductService productService;

    @Autowired
    private WareHouseService wareHouseService;


    @Override
    public int addSeckillSkuRelation(SeckillSkuRelationBo seckillSkuRelationBo) {
        return seckillSkuRelationDao.saveSeckillSkuRelation(seckillSkuRelationBo);
    }

    @Override
    public Integer getCount(Long promotionId, Long sessionId) {
        return seckillSkuRelationDao.getSeckillSkuCount(promotionId, sessionId);
    }

    @Override
    public PageUtils getSkuRelationList(Integer pageNum, Integer pageSize, Long promotionId, Long sessionId) {
        PageHelper.startPage(pageNum, pageSize);
        List<SeckillSkuRelationVo> seckillSkuRelationEntities = seckillSkuRelationDao.getSkuRelationList(promotionId, sessionId);
        List<Long> skuIds = seckillSkuRelationEntities.stream().map(SeckillSkuRelationVo::getSkuId).collect(Collectors.toList());
        // 查询关联的sku信息
        Map<Long, SkuInfo> skuListByIds = productService.getSkuListByIds(skuIds).getData();
        // 查询商品库存;
        Map<Long, Integer> mapStock = wareHouseService.getSeckillSkuStock(skuIds).getData();
        for (SeckillSkuRelationVo seckillSkuRelationEntity : seckillSkuRelationEntities) {
            SkuInfo skuInfo = skuListByIds.get(seckillSkuRelationEntity.getSkuId());
            seckillSkuRelationEntity.setSkuTitle(skuInfo.getSkuTitle());
            seckillSkuRelationEntity.setPrice(skuInfo.getPrice());
            seckillSkuRelationEntity.setStock(mapStock.get(seckillSkuRelationEntity.getSkuId()));
        }
        return new PageUtils(new PageInfo<>(seckillSkuRelationEntities));
    }

    @Override
    public void update(SeckillSkuRelationEntity seckillSkuRelation) {
        // todo  判断库存量
        seckillSkuRelationDao.update(seckillSkuRelation);
    }

    @Override
    public void delete(List<Long> ids) {
        // todo 要先判断是否已上架
        seckillSkuRelationDao.delete(ids);
    }

    @Override
    public List<SkuSeckillTo> regularSeckill() {
        // 凌晨3点请求到达，上架明天的秒杀商品
        LocalDate localDate = LocalDate.now().plusDays(1L);
        List<SkuSeckillTo> seckillTos = seckillSkuRelationDao.regularSeckill(localDate);
        return seckillTos;
    }
}